import java.security.SecureRandom;



public class Deck {

    public static final int DECK_SIZE = 52;
    public static int CARD_IN_DECK = 100;
    public static int CARD_RETURNED = -1;
    private int[] deck;
    private int cardLeft;
    SecureRandom Generator;
    /**
     * Deck is 
     */
    public Deck() {
        this.deck = new int[DECK_SIZE];
        for(int i =0; i<DECK_SIZE; i++)
        {
        	deck[i] = CARD_IN_DECK;
        }
        this.cardLeft = DECK_SIZE;
        this.Generator = new SecureRandom();
    }
    /**
     *unused 
     */
    public void printDeck () {
        for(int i = 0; i < 4; i++) {
            for(int j = 0; j < 13; j++) {
                System.out.print(deck[i*13+j] + " ");
            }
            System.out.print("\n");
        }
    }

    public void shuffle() {
        for(int i = 0; i < DECK_SIZE; i++) {
            if(deck[i] == CARD_RETURNED) {
                deck[i] = CARD_IN_DECK;
                cardLeft++;
            }
        }
    }
    public int giveCard(int player) {
        //System.out.println(cardLeft);
        if(cardLeft == 0)
            shuffle();
        int candidate = Generator.nextInt(DECK_SIZE);
        while(deck[candidate] != CARD_IN_DECK) {
            candidate = Generator.nextInt(DECK_SIZE);
        }
        deck[candidate] = player;
        cardLeft--;
        return candidate;
    }

    public void returnCardToDeck(int card) {
        if( card>=0 && card< DECK_SIZE)
    	deck[card] = CARD_RETURNED;
    }
    
    public int getOwnerNumber(int card) {
        return deck[card];
    }
    public void returnHandToDeck(Hand hand)
    {
    		      	int[] cards = hand.getCards();		
    		      	for(int i = 0; i < cards.length; i++)		
    	     	{		
    		      		deck[cards[i]]= CARD_RETURNED;		
    		       	}		
    	    }


}